<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'

export default {
  props: {
    LineData:Array,
    LineDataTimeList:Array,
    required: true,
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      cLineDataTimeList:[],
      cLineData:[]
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.chart.setOption({
        title: {
          // text: 'Stacked Line'
        },
        tooltip: {
          trigger: 'axis'
        },

        legend: {
          left: 'center',
          top: '40',
          show: true
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.cLineDataTimeList
        },
        yAxis: {
          type: 'value'
        },
        series: this.cLineData
      })
    }
  },
  watch:{
    LineData(newVal,oldVal){
      console.log(this.cLineData,12121)
      this.cLineData=JSON.parse(JSON.stringify(newVal).replace(/num_list/g, 'data').replace(/username/g, 'name'))

      this.cLineData.forEach((value,index)=>{
        value['type']='line'
        // value['stack']='Total'
        value['smooth']='true'
        value['symbol']='none'
      })
      // console.log(this.cLineData,12121)
      this.initChart()
    },
    LineDataTimeList(newVal,oldVal){
      this.cLineDataTimeList=newVal
      console.log(this.cLineDataTimeList,12121)
      // console.log(this.cLineDataTimeList,55555)
      this.initChart()
    }
  },
}
</script>
